package com.aggressionlabs.test.security;

import com.aggressionlabs.test.cache.UserCache;
import com.aggressionlabs.test.model.User;
import com.aggressionlabs.test.persistence.MongoSettings;
import com.aggressionlabs.test.security.model.ValidUser;
import org.eclipse.jetty.security.*;
import org.eclipse.jetty.server.UserIdentity;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.security.Credential;

import javax.security.auth.Subject;
import java.io.Serializable;
import java.security.Principal;

public class MongoLoginService extends AbstractLifeCycle implements LoginService{

    private static String _name = null;
    private UserCache _userCache = null;
    protected IdentityService _identityService = null;


    public MongoLoginService(String name){

        _identityService = new DefaultIdentityService();
        _name = name;

        _userCache =  new UserCache.Builder().server("localhost").build();

    }

    @Override
    public String getName() {
        return _name;
    }

    @Override
    public UserIdentity login(String s, Object o) {
        //check if user is in redis
        if ( _userCache.userIdExists(s)) { //user exists .. authenticate

            String[] roles = {"ADMIN","USER"};

            User u = _userCache.loadUser(s);
            ValidUser user = ValidUser.get(s);

            Credential c = Credential.getCredential((String)o);
            user.setCredential(c);
            //create subject
            Subject subject = new Subject();
            subject.getPrincipals().add(user);
            subject.getPrivateCredentials().add(c);

            for (String role: roles){
                subject.getPrincipals().add(new RolePrincipal(role));
            }


            if (UserCache.authenticate(user)){
                //UserIdentity identity = _identityService.newUserIdentity(subject,user,IdentityService.NO_ROLES);
                UserIdentity identity = _identityService.newUserIdentity(subject,user,roles);
               return identity;
            }
            else return null;

        }


       return null;
    }

    public static class RolePrincipal implements Principal,Serializable
    {
        private static final long serialVersionUID = 2998397924051854402L;
        private final String _roleName;
        public RolePrincipal(String name)
        {
            _roleName=name;
        }
        public String getName()
        {
            return _roleName;
        }
    }

    @Override
    public boolean validate(UserIdentity userIdentity) {
        return false;
    }

    @Override
    public IdentityService getIdentityService() {
        return _identityService;
    }

    @Override
    public void setIdentityService(IdentityService identityService) {
        _identityService = identityService;

    }

    @Override
    public void logout(UserIdentity userIdentity) {

    }


    @Override
    public void doStart(){
        System.out.println("Mongo login service starting");
    }
}
